package com.example.config;

import com.example.constants.Constants;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMQTTLConfig {
    //交换机名称
    private String ttlFanoutExchangeName = Constants.MY_TTL_FANOUT_EXCHANGE;

    //队列名称
    private String ttlQueueName = Constants.MY_TTL_QUEUE_NAME;

    @Bean
    public FanoutExchange ttlFanoutExchangeName() {
        FanoutExchange fanoutExchange = new FanoutExchange(ttlFanoutExchangeName);
        return fanoutExchange;
    }

    @Bean
    public Queue ttlQueueName() {
        Queue queue = new Queue(ttlQueueName);
        /**
         * 添加队列的参数
         */
        queue.addArgument("x-message-ttl", 30 * 1000);

        /**
         * 绑定死信队列
         */
        queue.addArgument("x-dead-letter-exchange",Constants.MY_DLX_DIRECT_EXCHANGE);
        queue.addArgument("x-dead-letter-routing-key","dlx");
        return queue;
    }

    @Bean
    public Binding binding5(Queue ttlQueueName, FanoutExchange ttlFanoutExchangeName) {
        //将队列绑定到交换机
        Binding binding = BindingBuilder.bind(ttlQueueName).to(ttlFanoutExchangeName);
        return binding;
    }
}